import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { InstallService } from '@services/data.service/index';
import { CacheService } from '@services/cache.service';
import { MyNoteService } from '@services/myNote.service';

@Component({
  selector: 'finish-step',
  templateUrl: './finish-step.html',
  styleUrls: ['finish-step.scss']
})
export class FinishComponent implements OnInit {
  @Input() roomInfo;
  @Input() installData;
  @Input() Step;
  @Output() NextNum: EventEmitter<any> = new EventEmitter();
  click = false; // 防止重复点击
  CodeInfo = {
    CodeInfo1: null,
    CodeInfo2: null,
    CodeInfo3: null,
    CodeInfo4: null
  };
  constructor(public modalCtrl: ModalController, public installService: InstallService,
    private cache: CacheService, private myNote: MyNoteService) {
  }

  // 初始页面显示
  ngOnInit() {
    console.log('完成页面房间信息', this.roomInfo, this.installData);
    this.CodeInfo.CodeInfo1 = [
      { name: '安装人员', value: this.installData.userName || this.cache.userinfo.userName },
      { name: '安装时间', value: this.installData.startTime + ' - ' + this.installData.endTime },
      { name: '安装位置', value: this.roomInfo.areaName + (this.installData.mapping || this.installData.data2 || '') }
    ];
    let txt;
    const word = [];
    txt = this.installData.data1 ? this.installData.data1.split(';') : null;
    for (let i = 0; i < txt.length; i++) {
      if (txt[i]) {
        txt[i] = txt[i].split(':');
        word.push({
          name: txt[i][0],
          value: txt[i][1]
        });
      }
    }
    this.CodeInfo.CodeInfo2 = word;
    this.CodeInfo.CodeInfo3 = this.installData.data5 ? this.installData.data5 : null;
    this.CodeInfo.CodeInfo4 = this.installData.data6 && this.installData.data6.CodeInfo3 ? this.installData.data6.CodeInfo3 : null;
    this.Step.state = 1;
    setTimeout(() => {
      this.click = true;
    }, 1500);
  }

  // 所有步骤完成
  toNext(i) {
    this.click = false;
    this.Step.now = i;
    let states = 0;
    this.installData.data7 = this.CodeInfo;
    // 判断是否有异常项
    if (this.installData.data7) {
      if (this.installData.data7.CodeInfo4) {
        for (let k = 0; k < this.installData.data7.CodeInfo4.length; k++) {
          if (this.installData.data7.CodeInfo4[k].check === true) {
            states++;
          }
        }
      }
    }
    if (states) {
      this.installData.state = 2;
    } else {
      this.installData.state = 3;
    }
    if (!this.roomInfo.device.isInstalled) {
      // 传送安装进度到后台
      console.log('完成安装进度', this.installData);
      let bType = null;
      let bId;
      if (this.installData.meterId === 0) {
        bType = 1;
        bId = this.installData.stackId ? this.installData.stackId : null;
      } else {
        bType = 2;
        bId = this.installData.meterId ? this.installData.meterId : null;
      }
      const step = this.installData.step || null;
      const userId = this.cache.userinfo.userId || null;
      const startTime = this.installData.startTime || null;
      const endTime = this.installData.endTime || null;
      const card = this.installData.data1;
      const address = this.installData.mapping;
      const deployFiles = [];
      // 初始值存储
      const deployParams = [];
      console.log(this.installData);
      if (this.installData &&  this.installData.data5 && this.installData.data5.length) {
        for (const data of this.installData.data5) {
          deployParams.push({ paramId: data.key, paramName: data.title, unit: data.unit, valueName: data.defaultName && data.defaultName.length ? data.defaultName[0] : null, value: data.defaultValue[0] })
        }
      }
      console.log('提交的数据', deployParams)
      // 校核参数存储
      const datas = this.installData.data6 && this.installData.data6.CodeInfo3 ? this.installData.data6.CodeInfo3 : null;
      this.installService.saveProgress(step, bType, bId, userId, card, address, startTime, endTime, deployFiles.length ? deployFiles : null, deployParams.length ? deployParams : null, datas).subscribe(data => {
        if (data.success) {
          console.log('提交安装进程成功并返回');
          this.NextNum.emit(this.Step.now);
        } else {
          this.myNote.MyNotice(data.message);
        }
      });
      setTimeout(() => {
        this.click = true;
      }, 1000);
    } else {
      this.NextNum.emit(this.Step.now);
      setTimeout(() => {
        this.click = true;
      }, 1000);
    }
  }
}
